#include <string.h>
#include <sys/types.h> 
#include <sys/socket.h> 
#include <stdio.h> 
#include <netinet/in.h> 
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>

#define BUFFER_SIZE 1024
#define PORT 3333
//const char *sock_str = "Code updates for warnings reported by Coverity scans Testing and warning fixes for FreeBSD on PowerPC Updates and sfdnkldjkljals";
const char *sock_str = "hello newpos";
char               buf[ 1024 ];

int main() 
{       
	pid_t pid;
	int server_sockfd, client_sockfd; 
       	int sin_size=sizeof(struct sockaddr_in); 
       	int recv_bytes = 0;
	struct sockaddr_in server_address; 
	struct sockaddr_in client_address; 
	memset(&server_address,0,sizeof(server_address));
	server_address.sin_family = AF_INET; 
	server_address.sin_addr.s_addr = INADDR_ANY; 
	server_address.sin_port = htons(PORT); 
	// 建立服务器端socket 
	if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0))<0)
	{
		perror("server_sockfd creation failed");
		exit(EXIT_FAILURE);
	}
	// 设置套接字选项避免地址使用错误
	int on=1;
	if((setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)))<0)
	{
		perror("setsockopt failed");
		exit(EXIT_FAILURE);
	}
	// 将套接字绑定到服务器的网络地址上 
	if((bind(server_sockfd,(struct sockaddr *)&server_address,sizeof(struct sockaddr)))<0)
	{
		perror("server socket bind failed");
		exit(EXIT_FAILURE);
	}
	// 建立监听队列
	listen(server_sockfd,500);
	while(1) {
	// 等待客户端连接请求到达
		client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,(socklen_t*)&sin_size);
		if(client_sockfd<0) {
			perror("accept client socket failed");
			exit(EXIT_FAILURE);
		} else {
			printf("client accepted: %d\n", client_address.sin_addr.s_addr);
			pid = fork();
			if (pid == 0) {
				printf("child process created\n");
				break;
			} else {
				printf("parent process continue\n");
			}
		}
	}
	int count = 0;
	while (1) {
		// 接收客户端数据
		if((recv_bytes = recv(client_sockfd,buf,16,0)) < 0)
		{
			perror("recv client data failed");
			exit(EXIT_FAILURE);
		}
		buf[recv_bytes] = 0;
		printf("recv<%d>: %s\n",count,buf);
		// 发送数据到客户端
		if(send(client_sockfd, sock_str, strlen(sock_str),0)<0) {
			perror("send failed");
			exit(EXIT_FAILURE);
		} else {
			printf("send<%d>: %s\n", count++, sock_str);
		}
		// usleep(1000*400);
	}
	close(client_sockfd);
	close(server_sockfd);
	exit(EXIT_SUCCESS);
}
